package kr.co.boogui.actions.movie;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.boogui.actions.ActionHandler;
import kr.co.boogui.dao.InfoCommentDAO;
import kr.co.boogui.dao.MovieDAO;
import kr.co.boogui.model.InfoCommentVO;

public class MovieInfoAction implements ActionHandler {
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
	
		String viewPage = "f|/WEB-INF/jsp/tiles/index.jsp";
		String method = req.getMethod();
		String movno = req.getParameter("movno");
		req.setCharacterEncoding("UTF-8");

		if (method.equalsIgnoreCase("get")) {
			
			req.setAttribute("action", "../movie/movie_info.jsp");

			req.setAttribute("movno", movno);
			req.setAttribute("movie_info", MovieDAO.viewMovie(movno));
			
			// 페이징 관련 변수 선언
			int cpage = 1, perpage = 20, startno = 0;
			// cpage,startno 계산
			if (req.getParameter("cpage") != null) {
				cpage = Integer.parseInt(req.getParameter("cpage"));

				startno = (cpage - 1) * perpage;
			}
			// 총페이지수 ,내비게이션 시작번호,글번호 변수
			int allpage, startnav = 1, startbno;			

			// 총페이지수 계산
			int total = InfoCommentDAO.totalBoard();
			allpage = (int) Math.ceil((float) total / perpage);

			// 내비게이션 시작번호 계산
			startnav = ((cpage - 1) / 10 * 10) + 1;

			// 글 번호 계산
			startbno = total - (perpage * (cpage - 1));

			viewPage = "f|/WEB-INF/jsp/tiles/index.jsp";
			req.setAttribute("action", "../movie/movie_info.jsp");

			// 게시판 관련 데이터들
			req.setAttribute("total", total);
			req.setAttribute("cpage", cpage);
			req.setAttribute("allpage", allpage);
			req.setAttribute("startnav", startnav);
			req.setAttribute("startbno", startbno);

			req.setAttribute("listBoards",
					InfoCommentDAO.listBoard(startno,movno));
		} else if (method.equalsIgnoreCase("post")) {
			
			req.setAttribute("action", "../movie/movie_info.jsp");


			InfoCommentVO ico = new InfoCommentVO();
			ico.setStars(req.getParameter("stars"));
			ico.setUid(req.getParameter("uid"));
			ico.setComment(req.getParameter("comment"));
			ico.setMovno(req.getParameter("movno"));

			InfoCommentDAO.writeBoard(ico);			
			
			//  -------------------------------------
			req.setAttribute("movno", movno);
			req.setAttribute("movie_info", MovieDAO.viewMovie(movno));
			
			// 페이징 관련 변수 선언
			int cpage = 1, perpage = 20, startno = 0;
			// cpage,startno 계산
			if (req.getParameter("cpage") != null) {
				cpage = Integer.parseInt(req.getParameter("cpage"));

				startno = (cpage - 1) * perpage;
			}
			// 총페이지수 ,내비게이션 시작번호,글번호 변수
			int allpage, startnav = 1, startbno;
			
			// 총페이지수 계산
			int total = InfoCommentDAO.totalBoard();
			allpage = (int) Math.ceil((float) total / perpage);

			// 내비게이션 시작번호 계산
			startnav = ((cpage - 1) / 10 * 10) + 1;

			// 글 번호 계산
			startbno = total - (perpage * (cpage - 1));

			viewPage = "f|/WEB-INF/jsp/tiles/index.jsp";
			req.setAttribute("action", "../movie/movie_info.jsp");

			// 게시판 관련 데이터들
			req.setAttribute("total", total);
			req.setAttribute("cpage", cpage);
			req.setAttribute("allpage", allpage);
			req.setAttribute("startnav", startnav);
			req.setAttribute("startbno", startbno);

			req.setAttribute("listBoards",
					InfoCommentDAO.listBoard(startno, movno));
			
		}
		return viewPage;
	}
}
